<?php

declare(strict_types=1);

namespace app\frontend\controller\product;

use app\frontend\controller\Base;
use think\App;
use think\facade\View;
use app\frontend\service\Category;
use app\frontend\service\Product;
use think\Request;
/**
 * @Create by vscode,
 * @author:jinxiu89@163.com
 * @Create Date:2020年07月24日 15:59:49 Friday
 * @User: admin
 * @Current File : index.php
 * @格言：溪涧岂能留得住，终归大海做波涛 --李忱
 * @格言： 我的内心因看见大海而波涛汹涌
 **/
class Index extends Base
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    public function index()
    {
        $data = (new Category())->getProductByCategory((int) $status = 1, (int) $this->language);
        View::assign('data', $data);
        return View::fetch($this->template . '/product/index.html');
    }

    /**
     * 从分类获取数据
     * @param Request $request
     * @return string
     */
    public function dataByCategory(Request $request)
    {
        if ($request->isGet()){
            $category_id = $request->param('category_id');
        }else{
            $category_name = $request->post('name');
            $category_id = (new Category())->cateidByname((int) $status = 1, (int) $this->language,$category_name);
        }
        $Category = new Category();
        //获取父级路径

        $parent_path = $Category->getPath((int) $status = 1, (int) $this->language,$category_id);
        $list = $Category->ProductDateByCate((int) $status = 1, (int) $this->language,$category_id);

        list($cateDate,$data) = $list;

        $page = $data->render();
        View::assign('parent_path', $parent_path);
        View::assign('cateDate', $cateDate);
        View::assign('data', $data);
        View::assign('page', $page);
        return View::fetch($this->template . '/product/index.html');
    }

    /**
     * 获取house列表下具体产品数据
     */
    public function dataByHouse(Request $request){
        $house_id = $request->param('house_id');
        //var_dump($house_id);exit();
        if (!empty($house_id)){
            //获取路径
            $category = new Category();
            $parent_path = $category->getPath((int) $status = 1, (int) $this->language,(int) $house_id);
            //获取house产品数据
            $list = $category->ProductDateByHouse((int) $status = 1, (int) $this->language,(int) $house_id);
            list($cateDate,$data) = $list;
            View::assign('parent_path', $parent_path);
            View::assign('cateDate', $cateDate);
            View::assign('data', $data);
            return View::fetch($this->template . '/product/index.html');
        }
    }

    public function detail($url)
    {
        $data = (new Product())->getDataByUrlTitle((string) $url, (int) $this->language);
        // print_r($data);
        View::assign('data', $data);
        return View::fetch($this->template . '/product/detail.html');
    }

    public function contact()
    {
        return View::fetch($this->template . '/product/Contact.html');
    }
}